﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Ivy.Common.Framework.GeneralUtility;
using Ivy.Common;


using Ivy.FeedbackModule.Entity;
using Ivy.FeedbackModule.IBusinessLogic;
using Ivy.FeedbackModule.BusinessLogic;
using Ivy.FeedbackModule.BusinessEntity;

using Ivy.UserRegistration.Entity;
using Ivy.UserRegistration.IBusinessLogic;
using Ivy.UserRegistration.BusinessLogic;

using DasIdeen.ViewEntity;


namespace DasIdeen.UserControls
{
    public partial class FeedbackPageControl : System.Web.UI.UserControl
    {
        #region  Variable & constant
        public AntiSpamUtility antiSpam = new AntiSpamUtility();
        #endregion  Variable & constant

        #region Constructor & Destructor
        #endregion Constructor & Destructor

        #region Properties
        #endregion Properties

        #region Public Methods
        #endregion Public Methods

        #region Events
        protected void page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                hidCorrectAnswer.Value = antiSpam.GenerateMathFormula();
                lblAntiSpam.Text = antiSpam.MathFormula;
            }
            GetCurrentUser();
            LoadFeedbacks();
        }

        protected void btnPost_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtAntiSpam.Text == hidCorrectAnswer.Value)
                {
                    FeedbackUser lFeedbackUser = new FeedbackUser();
                    Feedback lFeedback = new Feedback();
                    FeedbackModuleBus lFeedbackModuleBus = new FeedbackModuleBus();

                    TranslateUIControlsToEntity(lFeedbackUser, lFeedback);

                    lFeedbackModuleBus.SubmitFeedBack(lFeedbackUser,lFeedback);
                    Response.Redirect("~/Pages/FeedbackPage.aspx");
                    ClearScreen();
                   
                }
                else
                {
                    hidCorrectAnswer.Value = antiSpam.GenerateMathFormula();
                    lblAntiSpam.Text = antiSpam.MathFormula;
                }
            }

            catch (Exception ex)
            {
                ExceptionUtility.ExceptionHandler(ex, Server.MapPath("~/"));
            }
        }
        #endregion Events

        #region Private Methods
        private void CheckUserAutherization()
        {
        }

        private void ClearScreen()
        {
            this.txtName.Text = string.Empty;
            this.txtEmail.Text = string.Empty;
            this.FeedbackEditor.Content = string.Empty;
            this.txtAntiSpam.Text = string.Empty;
            
        }

        private void InitializeScreen()
        {
            ClearScreen();
            FillLoadLookUpData();
        }

        private void FillLoadLookUpData()
        {
        }

        private void TranslateEntityToUIControls()
        {
        }

        private void TranslateUIControlsToEntity(FeedbackUser pFeedbackUser, Feedback pFeedback)
        {
            pFeedbackUser.FeedbackUserName = txtName.Text.Trim();
            pFeedbackUser.FeedbackUserEmail = txtEmail.Text.Trim();

            pFeedbackUser.Sys_CreatedByID = "1";
            pFeedback.Sys_CreatedByID = "1";

            UserContext tempUserSession = (UserContext)Session["UserContext"];
            if (tempUserSession != null)
            {
                if (tempUserSession.CurrentUser != null)
                {
                    pFeedbackUser.Sys_CreatedByID = tempUserSession.CurrentUser.UserKey.ToString();
                    pFeedback.Sys_CreatedByID = tempUserSession.CurrentUser.UserKey.ToString();
                }
            }

            pFeedback.FeedbackText = FeedbackEditor.Content.Trim();
            
        }

        protected void LoadFeedbacks()
        {
            try
            {
                List<Feedbacks> lFeedbacks = new List<Feedbacks>();
                FeedbackModuleBus lFeedbackModuleBus = new FeedbackModuleBus();


                var vlstFeedback = lFeedbackModuleBus.GetAllUserFeedback();

                lFeedbacks = ((from x in vlstFeedback select x).OrderByDescending(x => x.FeedbackDate)).ToList();


                RptEstimationContentFeedback.DataSource = lFeedbacks;
                RptEstimationContentFeedback.DataBind();
            }
            catch (Exception ex)
            {
                ExceptionUtility.ExceptionHandler(ex, Server.MapPath("~/"));
            }
        }

        protected void grdFeedbackList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                List<Feedback> lstFeedback = new List<Feedback>();
                IFeedbackModuleBus lFeedbackModuleBus = new FeedbackModuleBus();
                if (Session["lstFeedback"] == null)
                {
                    var vlstFeedback = lFeedbackModuleBus.GetAllFeedback();
                    lstFeedback = ((from x in vlstFeedback select x).OrderByDescending(x => x.FeebackDate)).ToList();
                    Session["lstFeedback"] = lstFeedback;
                }
                else
                {
                    lstFeedback = (List<Feedback>)Session["lstFeedback"];
                }

                RptEstimationContentFeedback.DataSource = lstFeedback;
                //RptEstimationContentFeedback.PageIndex = e.NewPageIndex;
                RptEstimationContentFeedback.DataBind();
            }
            catch (Exception ex)
            {
                ExceptionUtility.ExceptionHandler(ex, Server.MapPath("~/"));
            }
        }

        private User GetCurrentUser()
        {
            User UserKey = null;
            if (Session["UserContext"] != null)
            {
                UserContext tempUserSession = (UserContext)Session["UserContext"];
                if (tempUserSession.CurrentUser != null)
                {
                    UserKey = tempUserSession.CurrentUser;
                    this.txtName.Text = tempUserSession.CurrentUser.UserFirstName;
                    this.txtEmail.Text = tempUserSession.CurrentUser.UserPrimaryEmailID;
                    this.txtName.ReadOnly = true;
                    this.txtEmail.ReadOnly = true;
                }
            }
            return UserKey;
        }
        #endregion Private Methods
        
    }
}
